var t = getApp(), e = t.requirejs('core');
Page({
    data: {
        data: {},
        total: 1,
        date: "",
        time: "",
        timeList: [],
        cid: '',
        id: '',
        did: '',
        detailList: [],
        selectDetail: {}
    },
    onLoad(s) {
        console.clear();
        e.get("syt.merch.credit_detail", s, res => {
            this.setData({data: res.cardinfo, id: s.id, detailList: res.details})
        })
    },
    changePicker(s) {
        let index = s.detail.value
        this.setData({
            selectDetail: this.data.detailList[index]
        })
    },
    // 增加数量
    add() {
        this.setData({
            total: this.data.total + 1
        })
    },
    // 减少数量
    sub() {
        if(this.data.total == 1) {
            wx.showToast({
                title: "最少为一个",
                icon: 'none'
            })
        } else {
            this.setData({
                total: this.data.total - 1
            })
        }
    },
    // 修改日期
    changDate(s) {
        let date = new Date();
        let now = new Date(date.getFullYear(), parseInt(date.getMonth()), date.getDate()).getTime();
        let select = new Date(s.detail.value.split('-')).getTime();
        if (select < now) {
            wx.showToast({
                title: '不能选择今天之前的日期',
                icon: 'none'
            })
            this.setData({
                date: `${date.getFullYear()}-${String(parseInt(date.getMonth()) + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
            })
            this.getTime();
            return
        }
        this.setData({
            date: s.detail.value
        })
        this.getTime();
    },
    // 获取时间
    getTime() {
        e.get("syt.merch.get_yue_time", {date: this.data.date}, res => {
            let list = res.yu.filter(item => {
                return item.can_yue == 1
            });
            this.setData({timeList: list})
            if(list.length == 0) {
                wx.showToast({title: '预约已满，请更换日期', icon: 'none'})
            }
        })
    },
    // 修改时间
    changeTime(s) {
        this.setData({
            time: s.detail.value
        })
    },
    // 提交
    submit(s) {
        if(!this.data.selectDetail.id) {
            wx.showToast({title: '请选择项目', icon: 'none'})
            return
        }
        if(!s.detail.value.date) {
            wx.showToast({title: '请选择日期', icon: 'none'})
            return
        }
        if(!s.detail.value.time) {
            wx.showToast({title: '请选择时间', icon: 'none'})
            return
        }
        let data = {
            id: this.data.id,
            did: this.data.selectDetail.id,
            cid: this.data.selectDetail.xmid,
            total: this.data.total
        }
        Object.assign(data, s.detail.value)
        data.time = this.data.timeList[s.detail.value.time].title
        e.get('syt.merch.tao_yue_submit', data, res => {
            console.log("提交", res)
            if(res.error != 0) {
                wx.showToast({title: res.message, icon: 'none'});
                return
            } else {
                wx.showToast({title: res.message, icon: 'none'});
                wx.switchTab({
                  url: '/pages/make/index'
                })
            }
        })
    }
})